from flask import session

from core.blueprints import api_blueprint
from core.blueprints.personal.models import PersonalInformation, PersonalIdentity
from core.lib.models import UpdateInstanceByDict
from core.lib.response import ResponsePostOne, ResponseGetOne, Response
from core.lib.status import isPassed
from models import insert

def get_information_instance(uid):
    return PersonalInformation.query.filter_by(uid=uid).first()

def get_identity_instance(uid):
    return PersonalIdentity.query.filter_by(uid=uid).first()

def update_information_instance(instance,json):
    UpdateInstanceByDict(instance, json)
    insert(instance)

def update_identity_instance(instance,json):
    UpdateInstanceByDict(instance, json)
    insert(instance)

@api_blueprint.route('/personal/PersonalInformation/<int:uid>', methods=['GET'])
def get_personal_information(uid):
    return ResponseGetOne(get_information_instance(uid))

@api_blueprint.route('/personal/PersonalInformation/role', methods=['POST'])
def update_personal_role():
    def cb(json):
        update_information_instance(get_information_instance(session['uid']),json)
        return Response("角色信息更新成功")
    return ResponsePostOne( ['role'], cb)

@api_blueprint.route('/personal/PersonalInformation/intro', methods=['POST'])
def update_personal_intro():
    def cb(json):
        update_information_instance(get_information_instance(session['uid']),json)
        return Response("个人介绍更新成功")
    return ResponsePostOne( ['intro'], cb)

@api_blueprint.route('/personal/PersonalInformation/img_icon', methods=['POST'])
def update_personal_img_icon():
    def cb(json):
        update_information_instance(get_information_instance(session['uid']),json)
        return Response("头像更新成功")
    return ResponsePostOne( ['img_icon'], cb)

@api_blueprint.route('/personal/PersonalInformation/img_background', methods=['POST'])
def update_personal_img_background():
    def cb(json):
        update_information_instance(get_information_instance(session['uid']),json)
        return Response("背景图片更新成功")
    return ResponsePostOne( ['img_background'], cb)

################################### identity

@api_blueprint.route('/personal/PersonalIdentity/<int:uid>', methods=['GET'])
def get_personal_identity(uid):
    return ResponseGetOne(get_identity_instance(uid))

@api_blueprint.route('/personal/PersonalIdentity/pass/<int:uid>', methods=['GET'])
def pass_personal_identity(uid):
    instance = get_identity_instance(uid)
    instance.checked = True
    instance.passed = True
    insert(instance)

    ins = PersonalInformation.query.filter_by(uid=uid).first()
    ins.role = instance.role
    insert(ins)
    return Response("已通过")

@api_blueprint.route('/personal/PersonalIdentity/unpass/<int:uid>', methods=['POST'])
def unpass_personal_identity(uid):
    def cb(json):
        instance = get_identity_instance(uid)

        instance.checked = True
        instance.passed = False
        instance.reason = json['reason']
        insert(instance)
        return Response("未通过, 原因:" + instance.reason)

    return ResponsePostOne(['reason'], cb)


@api_blueprint.route('/personal/PersonalIdentity/publish', methods=['POST'])
def publish_personal_identity():
    def cb(json):
        instance = get_identity_instance(session['uid'])

        if isPassed(instance.published,instance.checked,instance.passed):
            return Response("已通过认证,请勿提交")

        instance.published = True
        instance.checked = False
        instance.passed = False
        update_identity_instance(instance,json)
        return Response("已提交认证")

    return ResponsePostOne(['role','image'],cb)



